<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span>资源编号:</span>
        <el-input v-model="listQuery.resourceCode" clearable placeholder="请输入资源编号" />
      </div>
      <div class="filter-item">
        <span>数据权限名称:</span>
        <el-input v-model="listQuery.scopeName" clearable placeholder="请输入数据权限名称" />
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
      <!-- <el-button v-permission="['sys:datarule:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button> -->
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['sys:datarule:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :data="list" fit border highlight-current-row :row-class-name="tableRowClass" header-cell-class-name="header-cell">
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column label="资源编号" min-width="150px">
        <template #default="{row}">
          <span>{{ row.resourceCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据权限名称" min-width="150px">
        <template #default="{row}">
          <span>{{ row.scopeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据权限字段" min-width="150px">
        <template #default="{row}">
          <span>{{ row.scopeField }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据权限类型" min-width="150px">
        <template #default="{row}">
          <span>{{ dictLabel(row.scopeType, 'data_rule_type') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="230">
        <template #default="{row}">
          <el-button v-permission="['sys:datarule:update']" size="small" type="primary" plain @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-permission="['sys:datarule:delete']" size="small" plain type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <data-rule-form ref="form" @refresh-list="getList" />

  </div>
</template>

<script>
import { deleteDataRule, fetchDataRuleList } from '@/api/sys/dataRule/dataRule'
import dataRuleForm from './dataRuleForm'
import Pagination from '@/components/Pagination'
export default {
  name: 'DataRuleList',
  components: { dataRuleForm, Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        resourceCode: undefined,
        scopeName: undefined,
        page: 1,
        limit: this.$store.dictStore.defaultPageSize
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 刷新列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchDataRuleList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    /**
     * 删除
     * @param row 选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.$delAnim = true
        deleteDataRule([row.id]).then(response => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList(false)
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    /**
     * 新增
     */
    handleCreate() {
      this.$refs.form.handleCreate()
    }
  }
}
</script>
